package com.ojoin.trade.supplyloan_core.web.dao.impl;

import com.ojoin.trade.common.exception.BusinessException;
import com.ojoin.trade.common.exception.DaoException;
import com.ojoin.trade.common.utils.CommonUtils;
import com.ojoin.trade.common.utils.TimeUtils;
import com.ojoin.trade.common.web.dao.impl.BaseDaoImpl;
import com.ojoin.trade.supplyloan_core.web.dao.TbSlLoanInfoDao;
import com.ojoin.trade.supplyloan_core.web.domain.*;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 15-10-23
 * Time: 下午2:03
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class TbSlLoanInfoDaoImpl extends BaseDaoImpl<TbSlLoanInfo> implements TbSlLoanInfoDao {

    @Override
    public void insertSlLoanInfo(TbSlLoanInfo infovo) throws BusinessException {
        String mapMethodName ="insert";
        try{
            this.sqlSessionTemplate.insert(getSqlName(mapMethodName),infovo);
        }catch (Exception e) {
            throw new DaoException(String.format("插入商户贷款情况信息表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

    }

    @Override
    public int updateSlLoanInfo(Map<String,Object> paramsmap) throws BusinessException {

        String mapMethodName ="update";
        try{
           return this.sqlSessionTemplate.update(getSqlName(mapMethodName),paramsmap);
        }catch (Exception e) {
            throw new DaoException(String.format("更新商户贷款情况信息表出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public TbSlLoanInfo querySlLoanInfoByJoinid(int idOfTbMerchantUser) throws BusinessException {
        String mapMethodName = "selectByJoinId";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                    idOfTbMerchantUser);
            if (list != null && list.size() >0)
                return (TbSlLoanInfo) list.get(0);
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户贷款情况信息表出错->" + idOfTbMerchantUser + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
        return null;
    }



    @Override
    public List<TbSlLoanInfoVo> queryAllSlLoanInfoVo() throws BusinessException {
        String mapMethodName = "selectAllTbSlLoanInfo";
        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName));
            if (list != null && list.size() >0)
                return list ;
        } catch (Exception e) {
            throw new DaoException(String.format("查询用户申请供应链贷款记录出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
        return null;
    }

    @Override
    public void caculateFee() throws BusinessException {
        String mapMethodName ="caculateNotPayfee";
        try{
            this.sqlSessionTemplate.update(getSqlName(mapMethodName));
        }catch (Exception e) {
            throw new DaoException(String.format("商户贷款情况信息表计息出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

    }

    @Override
    public TbSlLoanInfo queryLoanInfoByJoinid(int idOfTbMerchantUser) throws BusinessException {

        String mapMethodName = "selectTotal";

        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                    idOfTbMerchantUser);
            if (list != null && list.size() >0)
                return (TbSlLoanInfo) list.get(0);
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户实时账户情况信息表出错->" + idOfTbMerchantUser + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
        return null;



    }

    @Override
    public long queryTotalNotPayback(int idOfTbMerchantUser) throws BusinessException {
        String mapMethodName = "selectsumnotpayback";
        Long amount=null;
        try {
             amount = this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),
                    idOfTbMerchantUser);
             if(CommonUtils.isEmpty(amount)){
                 return new Long(0);
             }
             return amount;
        } catch (Exception e) {
            throw new DaoException(String.format("查询商户实时负载金额出错->" + idOfTbMerchantUser + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<SlLoanNotPaybackVo> queryPlatformLoanInfoByJoinid(int idOfTbMerchantUser) throws BusinessException {
        String mapMethodName = "selectnotpaybackbyjoinid";

        try {
            List list = this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),
                    idOfTbMerchantUser);
          return list;
        } catch (Exception e) {                    ;
            throw new DaoException(String.format("查询商户平台贷款情况出错->" + idOfTbMerchantUser + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }

    }

    @Override
    public void lockid(int id) throws BusinessException {
        String mapMethodName = "selectLockid";

        try {
            this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),
                     id);
        } catch (Exception e) {
            throw new DaoException(String.format("加锁出错->" + id + "出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public void lockTable() throws BusinessException {
        String mapMethodName = "locktable";

        try {
            this.sqlSessionTemplate.selectList(getSqlName(mapMethodName));
        } catch (Exception e) {
            throw new DaoException(String.format("加锁出错->出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public void caculateTotalRenewedAmount() throws BusinessException {
        String mapMethodName ="caculateRenewedAmount";
        Map<String,Object> paramap=new HashMap<>();
        paramap.put("curdate", TimeUtils.getCurDate());
        try{
            this.sqlSessionTemplate.update(getSqlName(mapMethodName),paramap);
        }catch (Exception e) {
            throw new DaoException(String.format("商户贷款情况信息表未还展期服务费计息出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public void updateForCopySlloanInfo() throws BusinessException {
        String mapMethodName ="copyLoanInfoToStat";
        try{
            this.sqlSessionTemplate.update(getSqlName(mapMethodName));
        }catch (Exception e) {
            throw new DaoException(String.format("拷贝商户的贷款情况总表信息出错！语句：%s",
                    getSqlName(mapMethodName)), e);
        }
    }

    @Override
    public List<TbSlLoanDailyBillVo> queryDailyBill(String curdate,Integer idOfTbMerchantUser,Integer idOfTbMerchantUserOfCreditor,Pageable pageable) throws BusinessException {
        String mapMethodName="queryTodayBillVo";
        try{
        if(TimeUtils.getCurDate().equals(curdate)||CommonUtils.isEmpty(curdate)){
           mapMethodName="queryTodayBillVo";
            Map<String,Object> paramap=new HashMap<>();
            paramap.put("idOfTbMerchantUser",idOfTbMerchantUser);
            paramap.put("idOfTbMerchantUserOfCreditor",idOfTbMerchantUserOfCreditor);
            
            if (pageable == null) {
            	return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),paramap);
            } else {
            	return   this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),paramap,getPageBounds(pageable));
            }
        }else{
            mapMethodName="queryDailyBillVo";
            Map<String,Object> paramap=new HashMap<>();
            paramap.put("curdate",curdate);
            paramap.put("idOfTbMerchantUser",idOfTbMerchantUser);
            paramap.put("idOfTbMerchantUserOfCreditor",idOfTbMerchantUserOfCreditor);
            
            if (pageable == null) {
            	return this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),paramap);
            } else {
            	return   this.sqlSessionTemplate.selectList(getSqlName(mapMethodName),paramap,getPageBounds(pageable));
            }
        }}catch (Exception e){
            throw new DaoException(String.format("查询商户日报信息出错！语句：%s",
                    getSqlName(mapMethodName)), e) ;
        }
    }

    @Override
    public String queryDailySum(String curdate, Integer idOfTbMerchantUser,Integer idOfTbMerchantUserOfCreditor) throws BusinessException {
        String mapMethodName="queryTodaySum";
        try{
            if(TimeUtils.getCurDate().equals(curdate)||CommonUtils.isEmpty(curdate)){
                Map<String,Object> paramap=new HashMap<>();
                paramap.put("idOfTbMerchantUser",idOfTbMerchantUser);
                paramap.put("idOfTbMerchantUserOfCreditor",idOfTbMerchantUserOfCreditor);
                return   this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),paramap);
            }else{
                mapMethodName="queryDailySum";
                Map<String,Object> paramap=new HashMap<>();
                paramap.put("curdate",curdate);
                paramap.put("idOfTbMerchantUser",idOfTbMerchantUser);
                paramap.put("idOfTbMerchantUserOfCreditor",idOfTbMerchantUserOfCreditor);
                return    this.sqlSessionTemplate.selectOne(getSqlName(mapMethodName),paramap);
            }}catch (Exception e){
            throw new DaoException(String.format("查询商户的贷日报贷款总额信息出错！语句：%s",
                    getSqlName(mapMethodName)), e) ;
        }

    }
}
